package com.example.rememberpwd;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.TestLooperManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import static com.example.rememberpwd.SaveUserInfo.getUserInfo;

public class MainActivity extends AppCompatActivity {
//    声明控件
    private TextView et_acct, et_pwd;
    private Button btn_login;
    private String my_acct, my_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件
        et_acct = findViewById(R.id.et_act1);
        et_pwd = findViewById(R.id.et_pwd1);
        btn_login = findViewById(R.id.login_btn);
        //给按钮设置点击事件
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入信息
                my_acct = et_acct.getText().toString().trim();
                my_pwd = et_pwd.getText().toString().trim();
                //验证是否登录成功
                check(my_acct, my_pwd);
                //保存用户信息
                try {
                    save(my_acct, my_pwd);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        //如果用户保存了数据信息进行数据回显
        //            Map<String,String> userInfo = SaveUserInfo.getUserInfo(this);
        Map<String,String> userInfo = null;
        try {
            userInfo = SaveUserInfo.getUserInfo(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(userInfo!=null){
            et_acct.setText(userInfo.get("account"));
            et_pwd.setText(userInfo.get("password"));
        }
    }

    //检查输入是否为空

        private int check (String acct, String pwd){
            if (TextUtils.isEmpty(acct)) {
                Toast.makeText(this, "请输入账号！", Toast.LENGTH_LONG).show();
                return -1;
            }
            if (TextUtils.isEmpty(pwd)) {
                Toast.makeText(this, "密码不能为空！", Toast.LENGTH_LONG).show();
                return 0;
            }
            Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
            return 1;
        }

        //保存用户信息
        private void save (String my_acct, String my_pwd) throws IOException {
//        用文件操作保存密码
            boolean isSaveSuccess = SaveUserInfo.saveUserInfo(this,my_acct,my_pwd);
//            用SharedPreferences保存密码
//            boolean isSaveSuccess = SPsave.saveUserInfo(this,my_acct,my_pwd);
            if (isSaveSuccess) {
                Toast.makeText(this, "已保存账户密码:)", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "保存账户密码失败:(", Toast.LENGTH_LONG).show();
            }
        }
    }
